#include "StdAfx.h"
//#include "ListView.h"
//#include "AbstractItemModel.h"
//#include "AbstractItemDelegate.h"

namespace DuiLib
{
	IMPLEMENT_DUICONTROL(CListViewUI)

	CListViewUI::CListViewUI()
	{
	}

	CListViewUI::~CListViewUI()
	{
	}

	void CListViewUI::setModel(CAbstractItemModel* pModel)
	{
		CAbstractItemView::setModel(pModel);

		if (pModel)
		{
			showModel();
		}
	}

	void CListViewUI::onDataChanged(const CModelIndex& index,
		const CVar& oldValue, const CVar& newValue)
	{
		CAbstractItemModel* pModel = model();
		if (!pModel)
		{
			return;
		}

		CVar var = pModel->data(index);

		CControlUI* pItemCtrl = getItemWidget(index);
		if (pItemCtrl)
		{
			pItemCtrl->SetText(var.ToWideStr().c_str());
		}
	}

	void CListViewUI::onHeaderDataChanged(int section, Orientation orient,
		const CVar& oldValue, const CVar& newValue)
	{
		CAbstractItemModel* pModel = model();
		if (!pModel)
		{
			return;
		}

		CListHeaderUI* pHeader = GetHeader();
		if (!pHeader)
		{
			return;
		}

		CVar var = pModel->headerData(section, orient);
		CControlUI* pHeaderItem = pHeader->GetItemAt(section);
		if (pHeaderItem)
		{
			pHeaderItem->SetText(var.ToWideStr().c_str());
		}
	}

	void CListViewUI::onRowsBefroreInsert(int row, int count, const CModelIndex&)
	{

	}

	void CListViewUI::onRowsInserted(int row, int count, const CModelIndex&)
	{
		addLineToWidget();
	}

	void CListViewUI::onRowsBefroreRemove(int row, int count, const CModelIndex&)
	{
		for (int i = count; i > 0; i++)
		{
			RemoveAt(row + i);
		}
	}

	void CListViewUI::onRowsRemoved(int row, int count, const CModelIndex&)
	{
	}

	void CListViewUI::onRemoveAll(int rowCount)
	{
		RemoveAll();
	}

	void CListViewUI::showModel()
	{
		RemoveAll();

		CAbstractItemModel* pModel = model();
		if (!pModel)
		{
			return;
		}

		CListHeaderUI* pHeader = this->GetHeader();
		if (pHeader)
		{
			for (int col = 0; col < pModel->columnCount(); col++)
			{
				CControlUI* pCtrl = pHeader->GetItemAt(col);
				if (pCtrl)
				{
					CVar var = pModel->headerData(col, Orientation::Horizonal);
					if (var.GetType() != VTYPE_UNKNOWN)
					{
						pCtrl->SetText(var.ToWideStr().c_str());
					}
				}
			}
		}

		int lessCount = pModel->rowCount() - this->GetCount();
		for (int i = 0; i < lessCount; i++)
		{
			addLineToWidget();
		}

		for (int row = 0; row < pModel->rowCount(); row++)
		{
			for (int col = 0; col < pModel->columnCount(); col++)
			{
				CModelIndex&& index = model()->index(row, col);
				CControlUI* pCtrl = getItemWidget(index);
				if (pCtrl)
				{
					CVar var = pModel->data(index);
					pCtrl->SetText(var.ToWideStr().c_str());
				}
			}
		}
	}


	CControlUI* CListViewUI::getItemWidget(const CModelIndex& index)
	{
		CAbstractItemModel* pModel = model();
		if (!pModel)
		{
			return NULL;
		}

		CControlUI* pLineItem = this->GetItemAt(index.row());
		CContainerUI* pLineContainer = dynamic_cast<CContainerUI*>(pLineItem);
		if (pLineContainer)
		{
			return pLineContainer->GetItemAt(index.column());
		}

		return NULL;
	}

	void CListViewUI::OnSetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
	{
		CListUI::OnSetAttribute(pstrName, pstrValue);
	}

	CControlUI* CListViewUI::addLineToWidget()
	{
		CAbstractItemModel* pModel = model();
		if (!pModel)
		{
			return NULL;
		}

		CPaintManagerUI* pManager = this->GetManager();
		if (!pManager)
		{
			return NULL;
		}

		int columnCount = pModel->columnCount();
		columnCount  = (columnCount>GetColumnCount()) ? GetColumnCount() : columnCount;
		columnCount = columnCount == 0 ? 1 : columnCount;

		CListContainerElementUI* pListItem = new CListContainerElementUI();
		if (!pListItem)
		{
			return NULL;
		}

		pListItem->SetChildVAlign(DT_VCENTER);
		pListItem->SetFixedHeight(30);
		pListItem->SetManager(pManager, NULL, false);
		pListItem->SetFixedWidth(this->GetWidth() - 10);

		this->Add(pListItem);

		int lblWidth = pListItem->GetFixedWidth() / columnCount;
		int lblHeight = pListItem->GetFixedHeight();

		for (int i = 0; i < columnCount; i++)
		{
			CLabelUI* pLabel = new CLabelUI();
			if (pLabel)
			{
				pLabel->SetManager(pManager, NULL, false);
				pLabel->SetFixedHeight(lblHeight);
				pLabel->SetFixedWidth(lblWidth);
				pLabel->SetAttribute(_T("align"), _T("center,vcenter"));
				//pLabel->SetBkColor(0xFF00FF00);
				pListItem->Add(pLabel);
			}
		}

		return pListItem;
	}


	void CListViewUI::OnListItemClicked(int nIndex, int nColumn, RECT* lpRCColumn, LPCTSTR lpstrText)
	{
		if (nColumn < 0 || !model())
		{
			return;
		}

		CModelIndex&& modelIndex = model()->index(nIndex, nColumn);
		CControlUI* pItemCtrl = getItemWidget(modelIndex);
		CAbstractItemDelegate* pDelegate = itemDelegate(modelIndex);
		if (!pItemCtrl || !pDelegate)
		{
			return;
		}

		pDelegate->showEditor(pItemCtrl, modelIndex);
	}



}